<script setup lang="ts">
import {onLaunch} from '@dcloudio/uni-app'
import {useAppStore} from './stores/app'
import {useUserStore} from './stores/user'
import {useThemeStore} from './stores/theme'
import {useRoute, useRouter} from 'uniapp-router-next'
import {getToken} from "@/utils/auth";


const appStore = useAppStore()
const {getUser} = useUserStore()
const {getTheme} = useThemeStore()
const router = useRouter()
const route = useRoute()

//#ifdef H5
const setH5WebIcon = () => {
  const config = appStore.getWebsiteConfig
  let favicon: HTMLLinkElement = document.querySelector('link[rel="icon"]')!
  if (favicon) {
    favicon.href = config.h5_favicon
    return
  }
  favicon = document.createElement('link')
  favicon.rel = 'icon'
  favicon.href = config.h5_favicon
  document.head.appendChild(favicon)
}
//#endif

const getConfig = async () => {
  await appStore.getConfig()
  //#ifdef H5
  setH5WebIcon()
  //#endif
  const {status, page_status, page_url} = appStore.getH5Config
  if (route.meta.webview) return
  //处理关闭h5渠道
  //#ifdef H5
  if (status == 0) {
    if (page_status == 1) return (location.href = page_url)
    router.reLaunch('/pages/empty/empty')
  }
  //#endif
}

const connectSocket = async () => {
  return;
  const token = getToken()
  uni.connectSocket({
    url: 'wss://qxyhj.ybkj.club/wss',
    header: {
      'content-type': 'application/json',
      'token': token,
    },
    method: 'GET',
    success() {
      uni.onSocketOpen(function (res) {
        console.log('WebSocket连接已打开！');
        uni.sendSocketMessage({
          data: 'wo'
        });
        uni.onSocketMessage(function (res) {
          console.log('收到服务器内容：', JSON.parse(res.data));
        });
      });
      uni.onSocketError(function (res) {
        console.log('WebSocket连接打开失败，请检查！');
      });
    }
  });

}

onLaunch(async () => {
  getTheme()
  getConfig()
  //#ifdef H5
  setH5WebIcon()
  //#endif
  await getUser()
  connectSocket()
})
</script>
<style lang="scss">
// 自定义的一些公共样式
.page-box {
  margin: 30rpx auto;
  padding: 30rpx;
  border-radius: 20rpx;
  background-color: #ffffff;

  .box-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 30rpx;

    .top-name {
      font-size: 32rpx;
      font-weight: bold;
      color: #111111;
      padding-left: 30rpx;
      position: relative;

      &:before {
        content: '';
        position: absolute;
        left: 0;
        top: 50%;
        transform: translateY(-50%);
        width: 10rpx;
        height: 80%;
        border-radius: 10rpx;
        background-image: linear-gradient(to bottom, var(--color-primary), transparent);
      }
    }

    .top-more {
      display: flex;
      align-items: center;
      justify-content: flex-end;

      .more-text {
        font-size: 28rpx;
        color: #B8B8B8;
        margin-left: 10rpx;
      }

      .more-icon {
        margin-left: 10rpx;
      }
    }
  }
}

.page-line1 {
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
  word-break: break-all;
}
</style>
